// pages/musiclist/musiclist.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    playlistId:'',
    playlistDetail:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      playlistId:options.playlistId || 2829920189
    })
    this._getplaylistDetail()
  },
  /**
   * 获取歌单详情
   */
  async _getplaylistDetail() {
    try {
      wx.showLoading({
        title: '加载中',
      })
      let res = await wx.cloud.callFunction({
        name:'music',
        data:{
          $url:'playlistDetail',
          playlistId:this.data.playlistId
        }
      })
      if (res.errMsg.indexOf('ok') > -1) {
        this.setData({
          playlistDetail:res.result
        })
        this._setMusicList()
      } else {
        wx.showToast({
          title: '出错了,过一会再试',
        })
      }
    }catch(err) {
      console.log(`get playlist detail fail:${err}`)
    }finally {
      wx.hideLoading()
    }
  },
  /**
   * 设置音乐列表
   */
  _setMusicList() {
    wx.setStorageSync('musicList', this.data.playlistDetail.tracks)
  }
})